import java.awt.event.*;
import java.io.*;

class Control {
    private Widgets widgets = null;
    private String currFilePath = null;

    Control() {
        this.widgets = createGUI();
        this.setEvents();
    }

    public void startApp() {
        widgets.mainFrameShow();
    }

    private Boolean saveToFile(String text,String filePath) {
        FileWriter FW = null;
        Boolean res = false;
        try { 
            FW = new FileWriter(filePath);        
            FW.write(text);
            res = true;
        } catch (IOException e) {
            res = false;
        } finally {
            if (FW != null) {
                try {
                    FW.close(); 
                } catch (IOException e) {
                }
            }
        }
        return res;
    }

    private String loadFromFile(String filePath) {
        String buf,st = "";        
        BufferedReader bReader = null;
        try {
            bReader = new BufferedReader(new FileReader(filePath));
            while ( (buf = bReader.readLine()) != null) {
                st += (buf+"\n");
            }
        } catch(IOException e) {
            st = null;
        } finally {
            if (bReader != null) {
                try {
                    bReader.close();
                } catch (IOException e) {
                }
            }
        }
        return st;
    }
        
    private Widgets createGUI() {
        return new Widgets();
    }

    private void setEvents() {
        widgets.setOpenAction(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                String path = widgets.chooseFile("Open File");
                if (path != null) {
                    String str = loadFromFile(path);
                    if (str != null) {
                        widgets.setText(loadFromFile(path));
                    } else {
                        widgets.showErrorMessage("Can't open file");
                    }
                }
            }
        });

        widgets.setSaveAsAction(new ActionListener() {
             public void actionPerformed(ActionEvent ae) {
                 String path = widgets.chooseFile("Save File");
                 if (path != null) {
                     if ((new File(path)).exists()) {
                         if (widgets.getConfirmation("This file already exists." +"\n"
                                     + "Do you really want to overwrite it?")) {
                             if (saveToFile(widgets.getText(),path)) {
                                 widgets.showSuccessMessage("File was successfully saved");
                             } else {
                                 widgets.showErrorMessage("Can't save file");
                             }
                         }
                     }
                 }
             }
         });
   
        widgets.setExitAction(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    System.exit(0);
                }
            });
    }
}